Imports System.IO
Partial Class Budget_ExpenditureReportUpload
    Inherits System.Web.UI.Page

    Protected Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton.Click

        'Exit the procedure if nothing has been submitted.
        If FileUploadControl.FileName.Equals("") Then Exit Sub

        Dim ExpenditureReportDropLocation As String = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings.Get("ExpenditureReportDrop"))
        Try
            Dim Participant As Cdm.DataCore.Entities.Participant = EntityController.GetParticipant()
            If Not Participant Is Nothing Then
                If Not Directory.Exists(ExpenditureReportDropLocation & Participant.ParticipantId.ToString() & "\") Then
                    Directory.CreateDirectory(ExpenditureReportDropLocation & Participant.ParticipantId.ToString() & "\")
                End If
                FileUploadControl.SaveAs(ExpenditureReportDropLocation & Participant.ParticipantId.ToString() & "\" & FileUploadControl.FileName)

                Dim expLoader As New Spreadsheet.ExpenditureReport()
                Dim bCheckServiceAndItemCodes As Boolean = Not CBool(Application("UseDropdownsForItemAndServiceCodes"))
                Dim result As String = expLoader.UploadExpenditureReport(ExpenditureReportDropLocation & Participant.ParticipantId.ToString() & "\" & FileUploadControl.FileName, Participant.ParticipantId, UserAuthorization.CurrentUser.LogonId, bCheckServiceAndItemCodes)
                If result = "Invalid Service/Item Code(s)" Then
                    ResultLabel.Text = "Invalid Service/Item Code(s)"
                    ResultLabel.ForeColor = Drawing.Color.Red
                    ResultLabel.Visible = True
                Else
                    Dim spendingPlanId As Integer
                    spendingPlanId = Convert.ToInt32(result)
                    ResultLabel.Text = EntityController.ReplaceDisplayName("Monthly Statement") + " Uploaded Successfully!"
                    ResultLabel.ForeColor = Drawing.Color.Green
                    ResultLabel.Visible = True
                    Dim overUnder As Integer = Cdm.DataCore.Budget.ExpenditureReport.OverUnder(spendingPlanId)

                    EntityController.SendNotification(Cdm.DataCore.CaseNotes.Notification.NotificationType.MonthlyStatementSubmitted, Participant.ParticipantId)
                    If overUnder = 1 Then
                        EntityController.SendNotification(Cdm.DataCore.CaseNotes.Notification.NotificationType.BudgetAmountOverspent, Participant.ParticipantId)
                        'Else
                        '    If overUnder = -1 Then
                        '        EntityController.SendNotification(Cdm.DataCore.CaseNotes.Notification.NotificationType.BudgetAmountUnderspent, Participant.ParticipantId)
                        'End If
                    End If
                    Dim negativeBalance As Integer = Cdm.DataCore.Budget.ExpenditureReport.NegativeBalance(Participant.ParticipantId)
                If negativeBalance = 1 Then
                    EntityController.SendNotification(Cdm.DataCore.CaseNotes.Notification.NotificationType.NegativeAccountBudgetBalance, Participant.ParticipantId)
                End If
                End If
            End If
        Catch ex As Exception
            ResultLabel.Text = EntityController.ReplaceDisplayName("Monthly Statement") + " Upload Error: " + ex.Message
            ResultLabel.ForeColor = Drawing.Color.Red
            ResultLabel.Visible = True
        End Try
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If ddlPageView.SelectedValue = "standard" Then
            standarddiv.Visible = True
            customdiv.Visible = False
        Else
            standarddiv.Visible = False
            customdiv.Visible = True
        End If
        If Not Me.IsPostBack Then
            UserAuthorization.CheckPagePermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseMonthlyStatement, "INSERT")
            EntityController.SetSubNavTabsFinancial(Me.EditCaseSubNav2)
            EntityController.SetFinancialTab(Me.FinancialSubName, Me.EditCaseSubNav2)
            lblMonthlyStatement.Text = EntityController.ReplaceDisplayName("Monthly Statement")
            MonthlyStatementLabel.Text = "Upload " + EntityController.ReplaceDisplayName("Monthly Statement")
            MonthlyStatementLabelCustom.Text = "Upload " + EntityController.ReplaceDisplayName("Monthly Statement")
        End If
    End Sub

    Protected Sub EditCaseSubNav2_ChangeMenu(ByVal sender As Object, ByVal e As System.EventArgs) Handles EditCaseSubNav2.ChangeMenu
        EntityController.ReturnToCasePersonalPage(EditCaseSubNav2)
    End Sub

    Protected Sub SubmitButtonCustom_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButtonCustom.Click
        Dim ExpenditureReportDropLocation As String = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings.Get("ExpenditureReportDrop"))
        Dim filepath As String
        Try
            Dim Participant As Cdm.DataCore.Entities.Participant = EntityController.GetParticipant()
            If Not Participant Is Nothing Then
                If Not Directory.Exists(ExpenditureReportDropLocation & Participant.ParticipantId.ToString() & "\") Then
                    Directory.CreateDirectory(ExpenditureReportDropLocation & Participant.ParticipantId.ToString() & "\")
                End If
                filepath = ExpenditureReportDropLocation & Participant.ParticipantId.ToString() & "\" & FileUploadControlCustom.FileName
                FileUploadControl.SaveAs(filepath)
                'Dim result As String = Spreadsheet.ExpenditureReport.LoadExpenditureReport(ExpenditureReportDropLocation & FileUploadControl.FileName)
                Dim expenditureReport As New Cdm.DataCore.Budget.ExpenditureReport()
                expenditureReport.StartDate = Convert.ToDateTime(txtStatementStartDate.Value)
                expenditureReport.EndDate = Convert.ToDateTime(txtStatementEndDate.Value)
                Dim budget As Cdm.DataCore.Budget.Budget
                budget = Cdm.DataCore.Budget.Budget.RetrieveByParticipantId(Participant.ParticipantId)
                expenditureReport.BudgetId = budget.BudgetId
                expenditureReport.IsUploaded = True
                expenditureReport.Path = filepath
                expenditureReport.Persist()
                ResultLabelCustom.Text = EntityController.ReplaceDisplayName("Monthly Statement") + " Uploaded Successfully!"
                ResultLabelCustom.ForeColor = Drawing.Color.Green
                ResultLabelCustom.Visible = True
                EntityController.SendNotification(Cdm.DataCore.CaseNotes.Notification.NotificationType.MonthlyStatementSubmitted, Participant.ParticipantId)
            End If
        Catch ex As Exception
            ResultLabelCustom.Text = EntityController.ReplaceDisplayName("Monthly Statement") + " Upload Error: " + ex.Message
            ResultLabelCustom.ForeColor = Drawing.Color.Red
            ResultLabelCustom.Visible = True
        End Try
    End Sub
End Class
